package cn.itsource.order.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class ProductOrderController {

    @Autowired
    private IProductOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody Map<String,String> map, HttpServletRequest request){
        try {
            //获取token
            String token = request.getHeader("TOKEN");
            LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            //根据id 查询用户
            User user =  userService.loadByLoginInfoId(loginInfo.getId());

            String result = orderService.createOrder(map, user);
            return  AjaxResult.me().setResultObject(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }

    }

}
